<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  

  
  <title>CSS基础知识点--CSS定位(position) | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="定位模型 static：自然模型  relative：相对定位模型  absolute：绝对定位模型  fixed：固定定位模型  sticky：磁贴定位模型   static模型静态定位 / 常规定位 / 自然定位 特点：  忽略掉top、bottom、left、right 或者 z-index 声明  两个相邻的元素如果都设置了外边距，那么最终外边距 = 两者外边距中最大的  具有固定 wi">
<meta name="keywords" content="CSS">
<meta property="og:type" content="article">
<meta property="og:title" content="CSS基础知识点--CSS定位(position)">
<meta property="og:url" content="http://yoursite.com/2019/08/06/CSS基础知识点-CSS定位-position/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="定位模型 static：自然模型  relative：相对定位模型  absolute：绝对定位模型  fixed：固定定位模型  sticky：磁贴定位模型   static模型静态定位 / 常规定位 / 自然定位 特点：  忽略掉top、bottom、left、right 或者 z-index 声明  两个相邻的元素如果都设置了外边距，那么最终外边距 = 两者外边距中最大的  具有固定 wi">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-08-07T08:08:37.115Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="CSS基础知识点--CSS定位(position)">
<meta name="twitter:description" content="定位模型 static：自然模型  relative：相对定位模型  absolute：绝对定位模型  fixed：固定定位模型  sticky：磁贴定位模型   static模型静态定位 / 常规定位 / 自然定位 特点：  忽略掉top、bottom、left、right 或者 z-index 声明  两个相邻的元素如果都设置了外边距，那么最终外边距 = 两者外边距中最大的  具有固定 wi">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-CSS基础知识点-CSS定位-position" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2019/08/06/CSS基础知识点-CSS定位-position/" class="article-date">
  <time datetime="2019-08-06T13:48:01.000Z" itemprop="datePublished">2019-08-06</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/CSS/">CSS</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      CSS基础知识点--CSS定位(position)
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <hr>
<h2 id="定位模型"><a href="#定位模型" class="headerlink" title="定位模型"></a>定位模型</h2><ul>
<li><p>static：自然模型</p>
</li>
<li><p>relative：相对定位模型</p>
</li>
<li><p>absolute：绝对定位模型</p>
</li>
<li><p>fixed：固定定位模型</p>
</li>
<li><p>sticky：磁贴定位模型</p>
</li>
</ul>
<h2 id="static模型"><a href="#static模型" class="headerlink" title="static模型"></a>static模型</h2><p>静态定位 / 常规定位 / 自然定位</p>
<p>特点：</p>
<ol>
<li><p>忽略掉top、bottom、left、right 或者 z-index 声明</p>
</li>
<li><p>两个相邻的元素如果都设置了外边距，那么最终外边距 = 两者外边距中最大的</p>
</li>
<li><p>具有固定 width 和 height 值的元素，如果把左右外边距设置为 auto ，则左右外边距会自动扩大占满剩余宽度。造成的效果就是这个块居中</p>
</li>
</ol>
<h2 id="relative模型和absolute模型"><a href="#relative模型和absolute模型" class="headerlink" title="relative模型和absolute模型"></a>relative模型和absolute模型</h2><p>特点：</p>
<ol>
<li><p>relative模型（相对定位）：不脱离常规流</p>
</li>
<li><p>absolute模型（绝对定位）：脱离常规流</p>
</li>
<li><p>若父元素是相对定位，并且子元素是绝对定位，那么子元素的绝对定位是相对父元素</p>
</li>
<li><p>继上一个特点情况，若是想要实现子元素的块垂直或水平居中</p>
<ul>
<li><p>right 为 0 ，并且 left 为 0 ，margin 为 0 auto。实现水平居中</p>
</li>
<li><p>top 为 0 ，并且 bottom 为 0 ，margin 为 auto 0。实现垂直居中</p>
</li>
<li><p>top、right、bottom、left 均为 0，margin 为auto。实现水平垂直居中 </p>
</li>
<li><p>若 top、right、bottom、left 均为 auto，则会变为常规流（疑似有问题）</p>
</li>
</ul>
</li>
<li><p>定位的垂直水平居中，<font color="red">必须写定位模式</font></p>
<ul>
<li><p>需要给元素设置 top：50%，然后再 margin-top:负本身高度的一半。实现垂直居中</p>
</li>
<li><p>需要给元素设置 left：50%，然后再 margin-left:负本身宽度的一半。实现水平居中</p>
</li>
</ul>
</li>
</ol>
<h2 id="fix模型"><a href="#fix模型" class="headerlink" title="fix模型"></a>fix模型</h2><p>有些类似于 background-attachment 属性</p>
<h2 id="sticky模型（CSS3）"><a href="#sticky模型（CSS3）" class="headerlink" title="sticky模型（CSS3）"></a>sticky模型（CSS3）</h2><p>磁贴定位 / 粘性定位 / 吸附定位</p>
<p>作用：relative + fixed 的完美结合，制造出吸附效果</p>
<p>特点：</p>
<ol>
<li><p>如果产生偏移，原位置还是会在常规流中</p>
</li>
<li><p>如果它的最近父元素有滚动，那么它的偏移标尺就是最近的父元素</p>
</li>
<li><p>如果最近父元素没有滚动，那么它的偏移标尺是视口</p>
</li>
<li><p>上下左右的偏移规则</p>
</li>
<li><p>top等定位属性更像是条件，而非是坐标</p>
</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2019/08/06/CSS基础知识点-CSS定位-position/" data-id="ck4r400n60005l0w063r8r794" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/CSS/">CSS</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2019/08/08/CSS基础知识点-网页布局/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          CSS基础知识点--网页布局
        
      </div>
    </a>
  
  
    <a href="/2019/08/06/CSS基础知识点-float浮动/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">CSS基础知识点--float浮动</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Ajax/">Ajax</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/CSS/">CSS</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/CSS3/">CSS3</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Dart/">Dart</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Flutter/">Flutter</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Git/">Git</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/HTML/">HTML</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/HTML5/">HTML5</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JSON/">JSON</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/">JavaScript</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/QT/">QT</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/React/">React</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Sass/">Sass</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/TheKnowledgeOf/">TheKnowledgeOf</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/TypeScript/">TypeScript</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/bootstrap/">bootstrap</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/jQuery/">jQuery</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Ajax/">Ajax</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CSS/">CSS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CSS3/">CSS3</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Dart/">Dart</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Flutter/">Flutter</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Git/">Git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/HTML/">HTML</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/HTML5/">HTML5</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JSON/">JSON</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Javascript/">Javascript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/QT/">QT</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/React/">React</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Sass/">Sass</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TheKnowledgeOf/">TheKnowledgeOf</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TypeScript/">TypeScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/bootstrap/">bootstrap</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jQuery/">jQuery</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Ajax/" style="font-size: 12.22px;">Ajax</a> <a href="/tags/CSS/" style="font-size: 18.89px;">CSS</a> <a href="/tags/CSS3/" style="font-size: 17.78px;">CSS3</a> <a href="/tags/Dart/" style="font-size: 14.44px;">Dart</a> <a href="/tags/Flutter/" style="font-size: 16.67px;">Flutter</a> <a href="/tags/Git/" style="font-size: 12.22px;">Git</a> <a href="/tags/HTML/" style="font-size: 14.44px;">HTML</a> <a href="/tags/HTML5/" style="font-size: 12.22px;">HTML5</a> <a href="/tags/JSON/" style="font-size: 10px;">JSON</a> <a href="/tags/Java/" style="font-size: 11.11px;">Java</a> <a href="/tags/JavaScript/" style="font-size: 20px;">JavaScript</a> <a href="/tags/Javascript/" style="font-size: 10px;">Javascript</a> <a href="/tags/QT/" style="font-size: 12.22px;">QT</a> <a href="/tags/React/" style="font-size: 15.56px;">React</a> <a href="/tags/Sass/" style="font-size: 11.11px;">Sass</a> <a href="/tags/TheKnowledgeOf/" style="font-size: 11.11px;">TheKnowledgeOf</a> <a href="/tags/TypeScript/" style="font-size: 10px;">TypeScript</a> <a href="/tags/bootstrap/" style="font-size: 11.11px;">bootstrap</a> <a href="/tags/jQuery/" style="font-size: 13.33px;">jQuery</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/12/">December 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">May 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">April 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">March 2019</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2019/12/29/Flutter-MaterialApp常用属性介绍/">Flutter-MaterialApp常用属性介绍</a>
          </li>
        
          <li>
            <a href="/2019/12/29/Flutter-ListView/">Flutter-ListView</a>
          </li>
        
          <li>
            <a href="/2019/12/29/Flutter-AppBar/">Flutter-AppBar</a>
          </li>
        
          <li>
            <a href="/2019/12/29/Flutter-TabBar/">Flutter-TabBar</a>
          </li>
        
          <li>
            <a href="/2019/12/29/Flutter-按钮/">Flutter-按钮</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2019 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>